package com.ydcycloud.sys.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class FileUtil {

	/**
	 * 
	 * 保存视频文件到本地
	 * 
	 * @Description: TODO
	 * @param @param pathStr 视频访问的路径前部分，比如：http://localhost:8080/file/
	 * @param @param file 视频file
	 * @param @param extendName 扩展名
	 * @param @return
	 * @return Map<String,Object>
	 * @throws
	 * @author yzw
	 * @date 2017年7月26日
	 */
	@SuppressWarnings("resource")
	public static Map<String, Object> saveVideoFile(String pathStr, File file,
			String extendName) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String path = "C://video//";
		File f = new File(path);
		if (!f.exists()) {
			f.mkdir();
		}
		path = path + sdf.format(new Date()) + "//";
		f = new File(path);
		if (!f.exists()) {
			f.mkdir();
		}
		// 读取到的数据长度
		int len;
		// 1K的数据缓冲
		byte[] bs = new byte[1024];
		SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("HHmmss");
		String fileName = fileNameDateFormat.format(new Date())
				+ UUID.randomUUID().toString().substring(0, 8) + extendName; // 文件名
		OutputStream os = null;
		// 开始读取
		try {
			os = new FileOutputStream(path + fileName);
			InputStream inputStream = new FileInputStream(file);
			while ((len = inputStream.read(bs)) != -1) {
				os.write(bs, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("filePath", pathStr + sdf.format(new Date()) + "/" + fileName);
		return map;
	}

}
